package com.termine.logic;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.termine.bean.Bean;
import com.termine.entity.Location;
import com.termine.entity.PersonalData;
import com.termine.util.Logger;

@ManagedBean
@ApplicationScoped
public class PersonalDataManager extends Bean {

	public boolean addProvider(PersonalData personalData, Location location) {
		try {
			getLocationDAO().addLocation(location);
			personalData.setLocation_id(getLocationDAO().getLocationId(location));

			getPersonalDataDAO().addPersonalData(personalData);
			personalData.setPersonal_data_id(getPersonalDataDAO().getPersonalDataId(personalData));

			getPersonalDataDAO().addClient(personalData);
			personalData.setClient_id(getPersonalDataDAO().getClientId(personalData));
			getPersonalDataDAO().addProvider(personalData);
			personalData.setProvider_id(getPersonalDataDAO().getProviderId(personalData));

			getPersonalDataDAO().updatePersonalData(personalData);
			
			getLoginJSF().setPersonalData((PersonalData) personalData.clone());
			getLoginJSF().setLocation((Location) location.clone());
			return true;
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return false;
		}
	}

	public boolean addClient(PersonalData personalData, Location location) {
		try {
			getLocationDAO().addLocation(location);
			personalData.setLocation_id(getLocationDAO().getLocationId(location));

			getPersonalDataDAO().addPersonalData(personalData);
			personalData.setPersonal_data_id(getPersonalDataDAO().getPersonalDataId(personalData));

			getPersonalDataDAO().addClient(personalData);
			personalData.setClient_id(getPersonalDataDAO().getClientId(personalData));

			getPersonalDataDAO().updatePersonalData(personalData);
			
			getLoginJSF().setPersonalData((PersonalData) personalData.clone());
			getLoginJSF().setLocation((Location) location.clone());
			return true;
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return false;
		}
	}

}
